<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Warehouse\Controllers;


use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Warehouse;
use App\Eloquent\Ygt\WarehouseCart;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class CartController extends Controller
{
    public $userId          = 25;
    public function index(){}
    public function lists()
    {}
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $warehouseId        = request('warehouse_id',0);
        $number             = request('number',0);
        if( !$userInfo || !$warehouseId || !$number)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $isSubmit           = request('is_submit',0);
        if($isSubmit){
            $where                  = ['id'=>$warehouseId];
            $warehouse              = Warehouse::getInfo($where);
            if($warehouse){
                $customerId         = $warehouse->customer_id;
                $orderId            = $warehouse->order_id;
                $nowNumber          = $warehouse->now_number;
                if($number>$nowNumber)
                {
                    xThrow(ERR_PARAMETER, '数量超额');
                }
                $data               = [
                    'company_id'=>$companyId,'user_id'=>$userId,
                    'warehouse_id'=>$warehouseId,'customer_id'=>$customerId,
                    'order_id'=>$orderId,'number'=>$number,
                ];
                $where              = [
                    'warehouse_bill_id'=>0,
                    'customer_id'=>$customerId,
                    'warehouse_id'=>$warehouseId
                ];
                $warehouseCartInfo  = WarehouseCart::getInfo($where);
                $message            = '添加成功';
                if($warehouseCartInfo)
                {
                    $data['id']    = $warehouseCartInfo->id;
                    $message       = '编辑成功';
                }
                $fieldList              = WarehouseCart::$tableFieldList;
                $tableData              = WarehouseCart::setDataByTableField($data,$fieldList);
                $tableDataCheckResult   = WarehouseCart::checkDataByTableField($tableData,$fieldList);
                if($tableDataCheckResult !== true)
                {
                    xThrow(ERR_PARAMETER, $tableDataCheckResult);
                }
                $editResult             = WarehouseCart::edit($tableData);
                if($editResult===false)
                {
                    xThrow(ERR_UNKNOWN);
                }
                $result                 = ['message'=>$message];
                return $result;
            }else{
                xThrow(ERR_UNKNOWN);
            }
        } else {
            xThrow(ERR_UNKNOWN);
        }
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $type               = request('type',0);//1清空该客户的购物车
        $customerId         = request('customer_id',0);
        $warehouseId        = request('warehouse_id',0);
        $isRequest          = true;
        if($type==0 && $warehouseId==0)
        {
            $isRequest      = false;
        }
        if( !$userInfo || !$isRequest )
        {
            xThrow( ERR_PARAMETER );
        }
        if($type==1){
            $where              = ['warehouse_bill_id'=>0,'customer_id'=>$customerId];
            $delResult          = WarehouseCart::del($where);
        }else{
            $where              = ['warehouse_bill_id'=>0,'customer_id'=>$customerId,'warehouse_id'=>$warehouseId];
            $delResult          = WarehouseCart::del($where);
        }
        if(!$delResult)
        {
            xThrow(ERR_UNKNOWN);
        }
        return ['message'=>'删除成功'];
    }
}